package com.guyu.common.core.entity.page;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>分页返回数据</p>
 */
@Getter
@Accessors(chain = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PageData<T> {

    /**
     * 结果集
     */
    @Setter(AccessLevel.PUBLIC)
    private List<T> list = new ArrayList<>();
    /**
     * 总记录数
     */
    @Setter(AccessLevel.PUBLIC)
    private Long totalCount = 0L;
    /**
     * 页码，从1开始
     */
    @Setter(AccessLevel.PRIVATE)
    private Long pageNo = 1L;
    /**
     * 每页显示数量[1,1000]
     */
    @Setter(AccessLevel.PRIVATE)
    private Long pageSize = 10L;
    /**
     * 扩展信息
     */
    @Setter(AccessLevel.PUBLIC)
    private Map<String, Object> extra = new HashMap<>();

    public static <T> PageData<T> empty() {
        return new PageData<>();
    }

    public static <T> PageData<T> create(Long pageNo, Long pageSize) {
        return new PageData<T>().setPageNo(pageNo).setPageSize(pageSize);
    }

    public static <T> PageData<T> create(List<T> list, Long totalCount, Long pageNo, Long pageSize) {
        return create(list, totalCount, pageNo, pageSize, new HashMap<>());
    }

    public static <T> PageData<T> create(List<T> list, Long totalCount, Long pageNo, Long pageSize, Map<String, Object> extra) {
        return new PageData<>(list, totalCount, pageNo, pageSize, extra);
    }
}
